﻿<?php
include 'config.php';
//include("sotj.php");
$k=$_GET['k'];
$p=$_GET['p'];
$int=intval($p);
if($int<1){
	$p='1';
}
if($p==''){
	$p='1';
}



$k=urlencode($k);//这里必须使用URL编码,否则无法访问,应该是浏览器限制
$a=getHTTPS('https://www.xvideos.com/?k='.$k.'&p='.$p);
$dt=getSubstr($a,'<div id="content">','<div id="ad-footer">');
$dt=getSubstr($dt,'<div class="mozaique">','<a class="active" href="">');
$dt=str_replace('	','' ,$dt);
$dt=str_replace('<div class="thumb-inside">','' ,$dt);
$dt=str_replace('<div class="thumb">','' ,$dt);
$dt=str_replace(' class="thumb-block "','' ,$dt);
$dt=str_replace(array("\r\n", "\r", "\n"), '', $dt);
$dt=str_replace('</script>',"</script>\n\n\n" ,$dt);
$dt=str_replace('<div id=','{"vid":' ,$dt);
$dt=str_replace('"><a href="/video','","url":"'.$http.'/video' ,$dt);
$dt=str_replace('><img src=',',"imggif":' ,$dt);
$dt=str_replace(' data-src=',',"img":' ,$dt);
$dt=str_replace(' data-idcdn=',',"idcdn":' ,$dt);
$dt=str_replace(' data-videoid=',',"videoid":' ,$dt);
$dt=str_replace(' id=',',"id":' ,$dt);
$dt=str_replace('/></a>','' ,$dt);
$dt=str_replace(' </div></div><p>','' ,$dt);
$dt=str_replace('<a href="/video',',"url2":"/video' ,$dt);
$dt=str_replace('</div>','' ,$dt);
$dt=str_replace(' <span class="video-hd-mark">',',"hd":"' ,$dt);
$dt=str_replace('</span><p>','"' ,$dt);
$dt=str_replace(' class="icon-f icf-check-circle icf-white-fill prof-verified-tick"','' ,$dt);
$dt=str_replace(' title="Verified uploader" ','' ,$dt);
$dt=str_replace('href="/verified/videos"','' ,$dt);
$dt=str_replace(' title=',',"titel":' ,$dt);
$dt=str_replace('<p class="metadata">','' ,$dt);
$dt=str_replace('<span class="bg">','' ,$dt);
$dt=str_replace('</a></p><a href=','","form":' ,$dt);
$dt=str_replace('</span><a></a><p>','"' ,$dt);
$dt=str_replace('</a><span>','","vm":"' ,$dt);
$dt=str_replace(' <a></a><p>','' ,$dt);
$dt=str_replace('<span class="duration">','","time":"' ,$dt);
$dt=str_replace('</span>- ','' ,$dt);
$dt=str_replace('</span></span></p>','' ,$dt);
$dt=str_replace('xv.thumbs.prepareVideo','' ,$dt);
$dt=str_replace('<script>','","xvid":"',$dt);
$dt=str_replace('</script>','"}' ,$dt);
$dt=str_replace('<div class="pagination ">','' ,$dt);
$fy=getSubstr($dt,'<ul><li>','</li><li>');
$dt=str_replace($fy,'' ,$dt);
$dt=str_replace('<ul><li></li><li>','' ,$dt);
$dt=str_replace('</a></p>','","vm":"' ,$dt);


$dt=str_replace(array("\r\n", "\r", "\n"), '', $dt);
$dt=str_replace('}{','},{' ,$dt);

$dt='{"data":['.$dt.']}';

//$f=getSubstr($dt,'}<a','<li>]}');
//$dt=str_replace($f,'' ,$dt);
//$dt=str_replace('<a<li>','' ,$dt);

$dt=str_replace('}<a','},"tnnd":"' ,$dt);
$dt=str_replace(']}','"]}' ,$dt);
$dt=str_replace('href="','' ,$dt);
$dt=str_replace('</a>','' ,$dt);
$dt=str_replace('</li>','' ,$dt);
$dt=str_replace('<li>','' ,$dt);
$dt=str_replace('}"]}','}]}' ,$dt);
$dt=str_replace('<a','' ,$dt);
$dt=str_replace('" class="','' ,$dt);
$dt=str_replace('class="','' ,$dt);
$dt=str_replace('<li','' ,$dt);
$dt=str_replace('','' ,$dt);
$dt=str_replace('','' ,$dt);
$dt=str_replace('','' ,$dt);
$dt=str_replace('','' ,$dt);


$f=getSubstr($dt,',"tnnd":"',']}');
if(strlen($f)>300){
}
else{
$dt=str_replace($f,'' ,$dt);
}
//$dt=str_replace('<a<li>','' ,$dt);

$dt=str_replace('">','?/' ,$dt);
$dt=str_replace(',"tnnd":"','' ,$dt);
//echo $f;
echo $dt;


function getHTTPS($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_REFERER, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $result = curl_exec($ch);
  curl_close($ch);
  return $result;
}



























function postData($url, $data)
{
    $headers = array();
    $headers[] = 'Host: www.xvideos.com';
    $headers[] = 'Connection: keep-alive';
    $headers[] = 'Cache-Control: max-age=0';
    $headers[] = 'Upgrade-Insecure-Requests: 1';
    $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36';
    $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
    $headers[] = 'Accept-Encoding: gzip, deflate, sdch, br';
    $headers[] = 'Accept-Language: zh-CN,zh;q=0.8';
    $ch = curl_init();
    $timeout = 300;
    curl_setopt($ch, CURLOPT_URL, $url);
    //curl_setopt($ch, CURLOPT_REFERER, "http://127.0.0.1/");   //构造来路
	//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//构造http协议头
	
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $handles = curl_exec($ch);
    curl_close($ch);
    return $handles;
}
/*以下是取中间文本的函数 
getSubstr=调用名称
$str=预取全文本 
$leftStr=左边文本
$rightStr=右边文本
*/
function getSubstr($str, $leftStr, $rightStr)
{
    $left = strpos($str, $leftStr);
    //echo '左边:'.$left;
    $right = strpos($str, $rightStr,$left);
    //echo '<br>右边:'.$right;
    if($left < 0 or $right < $left) return '';
    return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr));
}
?>